package org.opens.sql.genHandle.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import org.opens.freemarker.FreemarkerUtil;
import org.opens.sql.constant.GenProjectPath;
import org.opens.exception.BusinessException;
import org.opens.sql.genConfig.GenSqlConfig;
import org.opens.sql.genHandle.GenHandle;
import org.opens.util.DatabaseConnectInfoEntity;
import org.opens.util.FileUtil;

import java.io.File;
import java.util.*;

/**
 * 接口空实现
 */
public class GenControllerHandle implements GenHandle {

    private static final String controllerFtlFilePath = "JavaController.ftl";

    @Override
    public void genCode(GenSqlConfig genSqlConfig) {
        if (StrUtil.isBlank(genSqlConfig.getGenServicePublicConfig().getPackageInfo())) {
            throw new BusinessException("未配置service包名, service终止生成!");
        }

        DatabaseConnectInfoEntity databaseConnectInfo = genSqlConfig.getDatabaseConnectInfo();

        String tableName = databaseConnectInfo.getTableName();

        // 首字母大写的驼峰命名格式的表名
        String ccTableName = StrUtil.upperFirst(StrUtil.toCamelCase(tableName));
        String serviceName = ccTableName + "Service";
        String controllerName = ccTableName + "Controller";
        String entityName = ccTableName + "Entity";

        String serviceImportPackageInfo = genSqlConfig.getGenServicePublicConfig().getPackageInfo() + "." + serviceName;

        Set<String> importSet = new HashSet<>();
        importSet.add("org.springframework.web.bind.annotation.*");
        importSet.add(serviceImportPackageInfo);

        if (genSqlConfig.getGenControllerPublicConfig().isAutowiredService()) {
            importSet.add("org.springframework.beans.factory.annotation.Autowired");
        }

        if (genSqlConfig.getGenControllerPublicConfig().isWithSlf4jAnnotation()) {
            importSet.add("lombok.extern.slf4j.Slf4j");
        }

        Map<String, Object> controllerConfigMap = BeanUtil.beanToMap(genSqlConfig.getGenControllerPublicConfig());
        Map<String, Object> ftlMap = new HashMap<>();
        ftlMap.putAll(controllerConfigMap);
        ftlMap.put("controllerName", controllerName);
        ftlMap.put("serviceName", serviceName);
        ftlMap.put("importSet", importSet);
        ftlMap.put("genControllerPublicConfig", genSqlConfig.getGenControllerPublicConfig());

        // 将包的包含.的路径转换为真实路径
        String controllerPath = StrUtil.replace(genSqlConfig.getGenControllerPublicConfig().getPackageInfo(), ".", File.separator);
        // 父目录
        String descParentPath = FileUtil.pathSplicing(genSqlConfig.getGenPath(), GenProjectPath.MAVEN_PROJECT_PREFIX, controllerPath);
        // java文件全路径
        String descPath = FileUtil.pathSplicing(descParentPath, StrUtil.upperFirst(StrUtil.toCamelCase(genSqlConfig.getDatabaseConnectInfo().getTableName())) + "Controller.java");

        // 创建目录
        FileUtil.createFolder(descParentPath);

        //GenSqlUtil.compileProcess(descPath, ftlMap, controllerFtlFilePath);
        FreemarkerUtil.compileProcess(descPath, ftlMap, controllerFtlFilePath);
    }


    @Override
    public void genCodeValid(GenSqlConfig genSqlConfig) {

    }
}
